﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Business.Resumes;
using iOak.Business.Campaigns;
using System.Web.Security;
using iOak.Domain.Campaigns;
using iOak.Domain.Resumes;

namespace iOak.Web.Administration
{
    public partial class Default : BasePage
    {
        private WorkAssistant _workassistant = new WorkAssistant();
        private CompanyService _companyService = new CompanyService();
        private ApplicationCandidateService _applicationCandidateService = new ApplicationCandidateService();
        private ResumeDetailService _resumeDetailService = new ResumeDetailService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindApplicationList();
                BindApplicationCheckList();
                hlMyTask.NavigateUrl = "";
            }
        }

        private void BindApplicationList()
        {
            ApplicationService applicationService = new ApplicationService();
            var dataSource = applicationService.GetListUsedByDefaultPage();
            rptApplicationList.DataSource = dataSource;
            rptApplicationList.DataBind();
            plNoData.Visible = dataSource.Count == 0;
        }

        private void BindApplicationCheckList()
        {
            ApplicationCandidateService applicationCandidateService = new ApplicationCandidateService();
            var dataSource = applicationCandidateService.GetListByCheckedBy(_workassistant.CurrentUser.UserId);
            rptApplicationCheckList.DataSource = dataSource ;
            rptApplicationCheckList.DataBind();
            plNoNotice.Visible = dataSource.Count == 0;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }

        protected void rptApplicationList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblApplication = e.Item.FindControl("lblApplication") as Label;
                HyperLink hlPreview = e.Item.FindControl("hlPreview") as HyperLink;
                ApplicationBase application = e.Item.DataItem as ApplicationBase;
                Company company = _companyService.Retrieve(application.CompanyId);
                if (company != null)
                {
                    int count = _applicationCandidateService.GetCount(applicationId: application.ApplicationId);
                    lblApplication.Text = String.Format("{0}提交了{1}个申请。", company.CompanyName, count.ToString());
                    hlPreview.NavigateUrl = "ApplicationCandidateList.aspx?applicationId=" + application.ApplicationId.ToString();
                }
                else 
                {
                    e.Item.Visible = false;
                }
            }
        }

        protected void rptApplicationCheckList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ApplicationCandidate applicationCandidate = e.Item.DataItem as ApplicationCandidate;
                var valueList = _resumeDetailService.GetListByResumeId(applicationCandidate.ResumeId);
                var userName = valueList.FirstOrDefault(item => item.Key.Equals("UserName")).ValueString;
                Label lblApplicationCandidate = e.Item.FindControl("lblApplicationCandidate") as Label;
                lblApplicationCandidate.Text = String.Format("候选人{0}，需要您审核。", userName);
                HyperLink hlCheck = e.Item.FindControl("hlCheck") as HyperLink;
                hlCheck.NavigateUrl = String.Format("/Administration/ApplicationCheck.aspx?applicationId={0}&applicationCandidateId={1}",
                                    applicationCandidate.ApplicationId.ToString(),
                                    applicationCandidate.ApplicationCandidateId.ToString());
            }
        }
    }
}